//
//	File Name   :	Dx10VertexFormat.cpp
//	Description :	VertexFormat implementation file.
//	Author		:	William McVicar
//  Mail		:	mcvicar.william@gmail.com
//

//  Precompiled Header
#include "CommonDefines.h"
#ifdef DX10

//  Library Includes

//  Local Includes
#include "ProteinFramework.h"
#include "IRenderer.h"
#include "RendererData.h"

//  This includes
#include "Dx10VertexFormat.h"

//	Macros

//  Static Variables

//  Static Function Prototypes

//  Implementation

namespace Protein
{

VertexFormat::VertexFormat( const VertexFormatBase* _pBase, LPD3D10BLOB _pVSBuf )
{
	memset( &m_elements, 0, sizeof( D3D10_INPUT_ELEMENT_DESC ) * 16 );
	for( u32 i = 0; i < _pBase->NumAttributes(); ++i )
	{
		D3D10_INPUT_ELEMENT_DESC elemDesc = { 0 };
		eSemanticType eType;
		eSemanticFormat eFormat;
		u32 uiOffset;
		_pBase->GetElement(i, uiOffset, eType, eFormat );
		switch( eType )
		{
			case ST_POSITION:
				elemDesc.SemanticName = "POSITION";
			break;

			case ST_COLOUR:
				elemDesc.SemanticName = "COLOR";
			break;

			case ST_NORMAL:
				elemDesc.SemanticName = "NORMAL";
			break;

			case ST_TEXCOORD:
				elemDesc.SemanticName = "TEXCOORD";
			break;
		}

		switch( eFormat )
		{
			case SF_FLOAT1:
				elemDesc.Format = DXGI_FORMAT_R32_FLOAT;
			break;

			case SF_FLOAT2:
				elemDesc.Format = DXGI_FORMAT_R32G32_FLOAT;
			break;

			case SF_FLOAT3:
				elemDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT;
			break;

			case SF_FLOAT4:
				elemDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
			break;
		}

		elemDesc.InputSlotClass	   = D3D10_INPUT_PER_VERTEX_DATA;
		elemDesc.AlignedByteOffset = uiOffset; 

		m_elements[i] = elemDesc;
	}

	HRESULT hr = renderer().GetRendererData()->m_pDevice->CreateInputLayout( m_elements, _pBase->NumAttributes(), _pVSBuf->GetBufferPointer(), _pVSBuf->GetBufferSize(), &m_pVertexLayout );
	PROTEIN_UNUSED( hr );
}

VertexFormat::~VertexFormat()
{

}

void VertexFormat::Enable()
{
	renderer().GetRendererData()->m_pDevice->IASetInputLayout( m_pVertexLayout );
}

void VertexFormat::Disable()
{
	//do nothing
}

}

#endif